package com.mlizhi.modules.spec.record.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.mlizhi.kingdom.R;
import com.mlizhi.utils.Constants;

import java.util.HashMap;
import java.util.List;

public class RecordPartAdapter extends BaseAdapter {

	private List<HashMap<String, String>> recordPartList;
	private LayoutInflater mInflater;
	private ViewHolder holder = null;
	private Context context;

	public RecordPartAdapter(Context context) {
		super();
		this.context = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	

	public List<HashMap<String, String>> getRecordPartList() {
		return recordPartList;
	}



	public void setRecordPartList(List<HashMap<String, String>> recordPartList) {
		this.recordPartList = recordPartList;
	}



	@Override
	public int getCount() {
		return recordPartList.size();
	}

	@Override
	public Object getItem(int position) {
		return recordPartList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HashMap<String, String> map = recordPartList.get(position);
		if ( convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.activity_record_category_item, null);
			
			holder.categoryImage = (ImageView) convertView.findViewById(R.id.id_category_image);
			holder.resultCompareLabel = (TextView) convertView.findViewById(R.id.id_result_compare_label);
			holder.conditonValue = (TextView) convertView.findViewById(R.id.id_conditon_value);
			holder.waterValValue = (TextView) convertView.findViewById(R.id.id_water_val_value);
			holder.resultCompareValue = (TextView) convertView.findViewById(R.id.id_result_compare_value);
			holder.resultCompareProcess =  (SeekBar) convertView.findViewById(R.id.id_compare_process);
//			holder.resultCompareProcessBg =  convertView.findViewById(R.id.id_compare_process_bg);
				
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int mapType = 0;
		try {
			mapType = Integer.parseInt(map.get(Constants.BODY_PART_TYPE));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			mapType = 0;
		}
		float waterValue = 20.0f;
		
		try {
			waterValue = Float.parseFloat(map.get(Constants.BODY_PART_WATER_VALUE));
			if(waterValue == 0.0f){
				waterValue = 20.0f;
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			waterValue = 20.0f;
		}
		
		if(Constants.BODY_PART_EYE == mapType){
			holder.categoryImage.setImageResource(R.drawable.ic_history_eye);
			if(Constants.BODY_PART_MIN <waterValue && waterValue <Constants.BODY_PART_EYE_MORMAL_MIN){
				holder.conditonValue.setText(R.string.skin_health4dry);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.dry_primary_color));
			}else if(Constants.BODY_PART_EYE_MORMAL_MIN <waterValue && waterValue <Constants.BODY_PART_EYE_MORMAL_MAX){
				holder.conditonValue.setText(R.string.skin_health4normal);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.normal_primary_color));
			}else if(Constants.BODY_PART_EYE_MORMAL_MAX <waterValue && waterValue <Constants.BODY_PART_MAX){
				holder.conditonValue.setText(R.string.skin_health4moist);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.moist_primary_color));
			}else {
				holder.conditonValue.setText(R.string.skin_health4none);
			}
		}else if(Constants.BODY_PART_FACE == mapType){
			holder.categoryImage.setImageResource(R.drawable.ic_history_face);
			if(Constants.BODY_PART_MIN <waterValue && waterValue <Constants.BODY_PART_FACE_MORMAL_MIN){
				holder.conditonValue.setText(R.string.skin_health4dry);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.dry_primary_color));
			}else if(Constants.BODY_PART_FACE_MORMAL_MIN <waterValue && waterValue <Constants.BODY_PART_FACE_MORMAL_MAX){
				holder.conditonValue.setText(R.string.skin_health4normal);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.normal_primary_color));
			}else if(Constants.BODY_PART_FACE_MORMAL_MAX <waterValue && waterValue <Constants.BODY_PART_MAX){
				holder.conditonValue.setText(R.string.skin_health4moist);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.moist_primary_color));
			}else {
				holder.conditonValue.setText(R.string.skin_health4none);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.font_primary_color));
			}
		}else if(Constants.BODY_PART_HAND == mapType){
			holder.categoryImage.setImageResource(R.drawable.ic_history_hand);
			if(Constants.BODY_PART_MIN <waterValue && waterValue <Constants.BODY_PART_HAND_MORMAL_MIN){
				holder.conditonValue.setText(R.string.skin_health4dry);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.dry_primary_color));
			}else if(Constants.BODY_PART_HAND_MORMAL_MIN <waterValue && waterValue <Constants.BODY_PART_HAND_MORMAL_MAX){
				holder.conditonValue.setText(R.string.skin_health4normal);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.normal_primary_color));
			}else if(Constants.BODY_PART_HAND_MORMAL_MAX <waterValue && waterValue <Constants.BODY_PART_MAX){
				holder.conditonValue.setText(R.string.skin_health4moist);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.moist_primary_color));
			}else {
				holder.conditonValue.setText(R.string.skin_health4none);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.font_primary_color));
			}
		}else if(Constants.BODY_PART_NECK == mapType){
			holder.categoryImage.setImageResource(R.drawable.ic_part_neck_pressed);
			if(Constants.BODY_PART_MIN <waterValue && waterValue <Constants.BODY_PART_NECK_MORMAL_MIN){
				holder.conditonValue.setText(R.string.skin_health4dry);
			}else if(Constants.BODY_PART_NECK_MORMAL_MIN <waterValue && waterValue <Constants.BODY_PART_NECK_MORMAL_MAX){
				holder.conditonValue.setText(R.string.skin_health4normal);
			}else if(Constants.BODY_PART_NECK_MORMAL_MAX <waterValue && waterValue <Constants.BODY_PART_MAX){
				holder.conditonValue.setText(R.string.skin_health4moist);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.moist_primary_color));
			}else {
				holder.conditonValue.setText(R.string.skin_health4none);
				holder.conditonValue.setTextColor(context.getResources().getColor(R.color.font_primary_color));
			}
		}
		
		holder.resultCompareLabel.setText(map.get(Constants.BODY_PART_COMPARE_LABEL));
		holder.waterValValue.setText(map.get(Constants.BODY_PART_WATER_VALUE));
		holder.resultCompareValue.setText(map.get(Constants.BODY_PART_COMPARE_VALUE));
//		holder.resultCompareProcess.setText(map.get(Constants.BODY_PART_COMPARE_PROCESS));
		
		holder.resultCompareProcess.setProgress((int) ((waterValue-20f)/40f *100f));
		holder.resultCompareProcess.setEnabled(false);
		return convertView;
	}

	static class ViewHolder {
		
		ImageView categoryImage;
		TextView resultCompareLabel;
		TextView conditonValue;
		TextView waterValValue;
		TextView resultCompareValue;
		SeekBar resultCompareProcess;
//		View resultCompareProcessBg;
	}

}
